# Maintainer: Andreas 'Segaja' Schleifer <archlinux at segaja dot de>

_gemname='bigdecimal'
pkgname="ruby-${_gemname}"
pkgver=3.1.2
pkgrel=5
pkgdesc='This library provides arbitrary-precision decimal floating-point number class'
arch=('loong64' 'x86_64')
url="https://github.com/ruby/bigdecimal"
license=('RUBY')
options=('!emptydirs')
depends=('ruby')
makedepends=('ruby-bundler' 'ruby-rake' 'ruby-rake-compiler')
checkdepends=('ruby-minitest')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('43aa08551bec0855223a0ffe787e62f67fc92824b766efc85fffc32f7b81e0850b199f0668fb0d23ed23e07e27030e25f885162bbe08489ea5df2978b69d4716')
b2sums=('0e2f8fd117116530f67d0e1830e607b90dcd630f0cd4d78ecfe11fa57c19e37223d424bb7608bdd6d94af3958e734c6df82374ee46146f3621cade3761c6d98c')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"
}

build() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"
  local _platform="$(gem env platform | cut -d':' -f2)"
  local _extension_api_version="$(ruby -e 'puts Gem.extension_api_version')"

  install --verbose --directory --mode=0755 \
    "tmp_install_default/gemspec/specifications/gems/${_gemname}-${pkgver}" \
    "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}" \
    "tmp_install${_gemdir}/specifications/default"

  gem build "${_gemname}.gemspec"

  gem install \
      --local \
      --verbose \
      --ignore-dependencies \
      --no-user-install \
      --install-dir "tmp_install/${_gemdir}" \
      --bindir "tmp_install/usr/bin" \
      "${_gemname}-${pkgver}.gem"

  gem install \
    --default \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install_default/gemspec" \
    --bindir "tmp_install_default/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  mv --verbose "tmp_install_default/gemspec/specifications/default/${_gemname}-${pkgver}.gemspec" "tmp_install${_gemdir}/specifications/default/${_gemname}-${pkgver}.gemspec"
  mv --verbose "tmp_install${_gemdir}/gems/${_gemname}-${pkgver}/lib/${_gemname}.rb" "tmp_install/usr/lib/ruby/${_extension_api_version}/${_gemname}.rb"
  mv --verbose "tmp_install${_gemdir}/gems/${_gemname}-${pkgver}/lib/${_gemname}" "tmp_install/usr/lib/ruby/${_extension_api_version}/${_gemname}"
  mv --verbose "tmp_install${_gemdir}/extensions/${_platform}/${_extension_api_version}/${_gemname}-${pkgver}/${_gemname}.so" "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}/${_gemname}.so"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install${_gemdir}/cache/" \
    "tmp_install${_gemdir}/build_info/" \
    "tmp_install${_gemdir}/extensions/" \
    "tmp_install${_gemdir}/gems/" \
    "tmp_install${_gemdir}/plugins/" \
    "tmp_install${_gemdir}/specifications/${_gemname}-${pkgver}.gemspec" \
    "tmp_install${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

}

check() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake test
}

package() {
  cd "${_gemname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}

# vim: tabstop=2 shiftwidth=2 expandtab:
